package cn.jarlen.jwt.tools.http;

import com.xdja.log.XdjaLog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jarlen
 * Create by 2023/6/12 16:54
 */
public class CheckNetLinkRunnable implements Runnable {
    public static final String TAG = CheckNetLinkRunnable.class.getSimpleName();

    private List<String> ip_portList;

    public CheckNetLinkRunnable() {
        ip_portList = new ArrayList<>();
        ip_portList.add("20.97.30.2:8443");
        ip_portList.add("20.96.27.17:80");
        ip_portList.add("20.95.17.2:8080");
    }

    public CheckNetLinkRunnable(List<String> ip_portList) {
        this.ip_portList = ip_portList;
    }

    @Override
    public void run() {
        if (ip_portList == null || ip_portList.isEmpty()) {
            return;
        }
        XdjaLog.w(TAG, "telnet--->Start");
        long lastTime = System.currentTimeMillis();
        while (true) {
            for (String str : ip_portList) {
                String[] ipPortSplit = str.split(":");
                String ip = ipPortSplit[0];
                int port = Integer.parseInt(ipPortSplit[1]);
                boolean telNetOk = TelnetManager.getInstance().connect(ipPortSplit[0], port, 2);
                XdjaLog.w(TAG, "telnet--->" + ip + ":" + port + ",  " + (telNetOk ? "OK" : "Fail"));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (System.currentTimeMillis() - lastTime > 10000) {
                XdjaLog.w(TAG, "telnet--->End");
                break;
            }
        }
    }
}
